#include <QPainter>
#include <QDebug>
#include <QMouseEvent>

#include "hexboard.h"



HexBoard::HexBoard(QWidget * parent) : QWidget(parent)
{
    init();
}

HexBoard::~HexBoard()
{

}


// 绘制六边形
// painter      QPainter对象
// x            像素横坐标
// y            像素纵坐标
void HexBoard::drawHexagon(QPainter & painter, double x, double y)
{
    static const QPointF hexagon[6] = {
        QPointF(1, 0),
        QPointF(0.5, 0.866),
        QPointF(-0.5, 0.866),
        QPointF(-1, 0),
        QPointF(-0.5, -0.866),
        QPointF(0.5, -0.866),
    };

    QPointF h[6];
    for(int i = 0; i < 6; i++)
    {
        h[i].setX(hexagon[i].x() * a + x);
        h[i].setY(hexagon[i].y() * a + y);
    }

    painter.drawConvexPolygon(h, 6);
    //painter.fillRect(0,0,100,100,COLOR_RED);
}


// 设置棋盘大小
// size         棋盘大小
void HexBoard::setSize(int size)
{
    this->size = size;

    init();
}


// 获取棋盘大小
// return       棋盘大小
int HexBoard::getSize()
{
    return size;
}


// 重载的paint事件
// event        事件参数
void HexBoard::paintEvent(QPaintEvent * event)
{

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            painter.setBrush(colors[i][j]);
            painter.setPen(Qt::NoPen);
            drawHexagon(painter, left + (i + j) * 1.5 * (a + gap), top + (i - j) * 0.866 * (a + gap));
        }
    }
    painter.setPen(Qt::gray);
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            double cx = left + (i + j) * 1.5 * (a + gap);
            double cy = top + (i - j) * 0.866 * (a + gap);
            painter.drawText(cx - 15, cy - 15 , 30, 30, Qt::AlignCenter, QString("%1").arg(i * size + j));
        }
    }

    if (this->displayAxis)
    {
        painter.setPen(Qt::red);
        for(int i = 0 ; i < size; i++)
        {
            double cx = left + (-1 + i) * 1.5 * (a + gap);
            double cy = top + (-1 - i) * 0.866 * (a + gap);
            painter.drawText(cx - 15, cy - 15 , 30, 30, Qt::AlignCenter, QString("%1").arg(i));
        }
        painter.setPen(Qt::blue);
        for(int i = 0; i < size; i++)
        {
            double cx = left + (i + -1) * 1.5 * (a + gap);
            double cy = top + (i - -1) * 0.866 * (a + gap);
            painter.drawText(cx - 15, cy - 15 , 30, 30, Qt::AlignCenter, QString("%1").arg(i));
        }
    }

    //painter.fillRect(0,0,100,100,COLOR_RED);
}


// 设置棋盘绘制的原点（即棋盘最左边的点中心的坐标）
// left         坐标left
// top          坐标top
void HexBoard::setOrigin(int left, int top)
{
    this->left = left;
    this->top = top;
}


// 重载的鼠标事件
// event        事件参数
void HexBoard::mousePressEvent(QMouseEvent * event)
{
    double x = event->pos().x();
    double y = event->pos().y();

    x -= left;
    y -= top;
    //qDebug() << x << y;

    //int tx = floor(x / (1.5 * a));
    //int ty = floor(y / (0.866 * a));

    double d1 = (x + 1.73205 * y) / 2.0;
    double d2 = (x - 1.73205 * y) / 2.0;

    int nx = floor(d1 / (1.5 * (a + gap)));
    int ny = floor(d2 / (1.5 * (a + gap)));
    //qDebug() << "nx, ny: " << nx << ny;

    double tx;
    double ty;
    double s;
    double shortest = 0x3f3f3f3f;
    int ansx = 0, ansy = 0;

    tx = 1.5 * (a + gap) * (nx + ny);
    ty = 0.866 * (a + gap) * (ny - nx);
    s = pow(x - tx, 2) + pow(-y - ty, 2);
    if(s < shortest)
    {
        ansx = nx;
        ansy = ny;
        shortest = s;
    }
    tx = 1.5 * (a + gap) * (nx + ny + 1);
    ty = 0.866 * (a + gap) * (ny - nx + 1);
    s = pow(x - tx, 2) + pow(-y - ty, 2);
    if(s < shortest)
    {
        ansx = nx;
        ansy = ny + 1;
        shortest = s;
    }
    tx = 1.5 * (a + gap) * (nx + ny + 2);
    ty = 0.866 * (a + gap) * (ny - nx);
    s = pow(x - tx, 2) + pow(-y - ty, 2);
    if(s < shortest)
    {
        ansx = nx + 1;
        ansy = ny + 1;
        shortest = s;
    }
    tx = 1.5 * (a + gap) * (nx + ny + 1);
    ty = 0.866 * (a + gap) * (ny - nx - 1);
    s = pow(x - tx, 2) + pow(-y - ty, 2);
    if(s < shortest)
    {
        ansx = nx + 1;
        ansy = ny;
    }

    if(ansx >= 0 && ansx < size && ansy >= 0 && ansy < size)
    {
        emit click(ansx, ansy);
    }
}


// 设置棋盘某一格的颜色
// x            棋盘横坐标
// y            棋盘纵坐标
// color        QColor对象，描述一种颜色
void HexBoard::setGridColor(int x, int y, QColor color)
{
    this->colors[x][y] = color;

    // 设置完成后需手动更新
    //this->update();
}


// 初始化棋盘、颜色
void HexBoard::init()
{
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            colors[i][j].setRgb(200, 200, 200);
        }
    }
}

void HexBoard::setGridColor(int pos, QColor color)
{
    this->setGridColor(pos / this->size, pos % this->size, color);
}

void HexBoard::clearColor()
{
	for (int i = 0; i < this->size; i++)
	{
		for (int j = 0; j < this->size; j++)
		{
			this->colors[i][j] = Qt::white;
		}
	}
}
